package com.kingdee.eas.custom.rcbudget.util;

import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

import com.kingdee.bos.ctrl.swing.KDTree;
import com.kingdee.bos.ctrl.swing.tree.DefaultKingdeeTreeNode;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.ui.face.ItemAction;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.NewOrgUtils;
import com.kingdee.eas.basedata.org.OrgStructureInfo;
import com.kingdee.eas.basedata.org.OrgViewType;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.fdc.basedata.client.FDCClientUtils;
import com.kingdee.eas.framework.client.CoreUI;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.MsgBox;

public class TreeBuilder {

	public void build(CoreUI ui, KDTree projectTree, ItemAction actionOnLoad)
			throws Exception {
		final KDTree prjTree = projectTree;
		build(ui, prjTree, actionOnLoad, null);
	}

	public void build(CoreUI ui, KDTree projectTree, ItemAction actionOnLoad,
			String curOrgId) throws Exception {
		this.ui = ui;
		if (curOrgId == null && !noOrgIsolation)
			curOrgId = SysContext.getSysContext().getCurrentOrgUnit().getId()
					.toString();
		TreeModel orgTreeModel = NewOrgUtils.getTreeModel(OrgViewType.COMPANY,
				"", curOrgId, null, getActionPK(actionOnLoad));
		DefaultKingdeeTreeNode root = (DefaultKingdeeTreeNode) orgTreeModel
				.getRoot();
		if (root.getUserObject() != null) {
			OrgStructureInfo orgStru = (OrgStructureInfo) root.getUserObject();
			if (!orgStru.getUnit().isIsCostOrgUnit()) {
				MsgBox.showWarning(FDCClientUtils.getRes("needCostcenter"));
				SysUtil.abort();
			}
			removeNoneCostCenterNode(root);
			buildByOrgTree(projectTree, orgTreeModel);
		}
	}

	private void removeNoneCostCenterNode(DefaultKingdeeTreeNode node) {
		for (int i = 0; i < node.getChildCount(); i++) {
			DefaultKingdeeTreeNode curNode = (DefaultKingdeeTreeNode) node
					.getChildAt(i);
			OrgStructureInfo orgStru = (OrgStructureInfo) curNode
					.getUserObject();
			if (!orgStru.getUnit().isIsCostOrgUnit()) {
				node.remove(i);
				i--;
				continue;
			}
			if (curNode.getChildCount() > 0)
				removeNoneCostCenterNode(curNode);
		}
	}

	public TreeBuilder(boolean isIncludeDisabled) {
		noOrgIsolation = false;
	}

	public void buildByOrgTree(KDTree orgTree, TreeModel orgTreeModel)
			throws Exception {
		orgTree.setModel(orgTreeModel);
		TreeNode orgRoot = (TreeNode) orgTreeModel.getRoot();
		int nodes = orgRoot.getChildCount();
		for (int i = 0; i < nodes; i++) {
			DefaultKingdeeTreeNode orgNode = (DefaultKingdeeTreeNode) orgRoot
					.getChildAt(i);
			if (orgNode.getChildCount() > 0) {
				buildProTree(orgNode);
			} else {
				OrgStructureInfo orgStru = (OrgStructureInfo) orgNode.getUserObject();
				FullOrgUnitInfo info = orgStru.getUnit();
				DefaultKingdeeTreeNode proNode = new DefaultKingdeeTreeNode(
						info);
				orgNode.add(proNode);
			}
		}
		if (nodes <= 0) {
			DefaultKingdeeTreeNode orgNode = (DefaultKingdeeTreeNode) orgRoot;
			OrgStructureInfo orgStru = (OrgStructureInfo) orgNode
					.getUserObject();
			FullOrgUnitInfo info = orgStru.getUnit();
			
			DefaultKingdeeTreeNode proNode = new DefaultKingdeeTreeNode(
					info);
			orgNode.add(proNode);
		}
	}

	private void buildProTree(DefaultKingdeeTreeNode orgRoot) throws Exception {
		int nodes = orgRoot.getChildCount();
		for (int i = 0; i < nodes; i++) {
			DefaultKingdeeTreeNode orgNode = (DefaultKingdeeTreeNode) orgRoot
					.getChildAt(i);
			OrgStructureInfo orgStru = (OrgStructureInfo) orgNode
					.getUserObject();
			if (orgNode.getChildCount() > 0) {
				buildProTree(orgNode);
			} else {
				FullOrgUnitInfo info = orgStru.getUnit();
				
				DefaultKingdeeTreeNode proNode = new DefaultKingdeeTreeNode(info);
				orgNode.add(proNode);
			}
		}
	}

	private IMetaDataPK getActionPK(ItemAction action) {
		if (action == null)
			return null;
		String actoinName = action.getClass().getName();
		if (actoinName.indexOf("$") >= 0)
			actoinName = actoinName.substring(actoinName.indexOf("$") + 1);
		return new MetaDataPK(actoinName);
	}

	private CoreUIObject ui;
	private boolean noOrgIsolation;
}